package com.example.demo.mqserver.core;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.HashMap;
import java.util.Map;


public class Exchange {
    private String name;
    private ExchangeType type = ExchangeType.DIRECT;
    private boolean durable = false;
    private boolean autoDelete = false;
    // 将该类型转换为json字符才能保存在数据库中
    private Map<String, Object> arguments = new HashMap<>();

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public ExchangeType getType() {
        return type;
    }

    public void setType(ExchangeType type) {
        this.type = type;
    }

    public boolean isDurable() {
        return durable;
    }

    public void setDurable(boolean durable) {
        this.durable = durable;
    }

    public boolean isAutoDelete() {
        return autoDelete;
    }

    public void setAutoDelete(boolean autoDelete) {
        this.autoDelete = autoDelete;
    }
    private ObjectMapper objectMapper = new ObjectMapper();
    public String getArguments() {
        // 将arguments参数,从map转换为string
        try {
            return objectMapper.writeValueAsString(arguments);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return "{}";
    }

    public void setArguments(String argumentsJSON) {
        try {
            // 第二个参数用来描述当前的json字符串要转成的java对象是啥类型的 , 如果是个简单类型,直接对应简单类对象即可
            // 如果是复杂对象,则需要通过匿名内部类typereference
            this.arguments = objectMapper.readValue(argumentsJSON,
                    new TypeReference<HashMap<String, Object>>() {});

        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }
    public void setArguments(HashMap<String,Object> arguments) {
        this.arguments = arguments;
    }

    public Object getArguments(String key) {
        return arguments.get(key);
    }


}
